package com.hpq.common.event.listener;

import com.hpq.common.event.FriendApplyEvent;
import com.hpq.user.dao.UserApplyDAO;
import com.hpq.user.domain.model.UserApply;
import com.hpq.user.domain.vo.response.websocket.WSFriendApply;
import com.hpq.user.service.PushService;
import com.hpq.user.service.adapter.WSAdapter;
import com.hpq.websocket.service.WebSocketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

@Component
@Slf4j
public class FriendApplyListener {
    @Autowired
    private UserApplyDAO userApplyDao;
    @Autowired
    private WebSocketService webSocketService;
    @Autowired
    private PushService pushService;
    @Async
    @TransactionalEventListener(classes = FriendApplyEvent.class,fallbackExecution = false)
    public void notifyFriendApply(FriendApplyEvent event){
        UserApply userApply = event.getUserApply();
        Integer unReadCount = userApplyDao.getUnReadCount(userApply.getTargetId());
        pushService.sendPushMsg(WSAdapter.buildApplySend(new WSFriendApply(userApply.getUid(),unReadCount)),userApply.getTargetId());
    }
}
